package managedbean.TX.SABM;

import session.TX.TierManagementSBRemote;
import entity.StakeholderTier;
import exception.ExistException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "tierManagerBean")
@SessionScoped
public class TierMB {

    @EJB
    TierManagementSBRemote tierManagementBean;
    String name;
    Integer commissionPercent;
    Double feeSubscription;
    Double feeMarketIntelligence;
    Double costPerClick;
    String statusMessage;
    private StakeholderTier selectedTier;


    public TierMB() {}

    public void createNewTier(ActionEvent event) {
        String tierName = String.valueOf(getName());
        Integer commissionPercentage = Integer.valueOf(getCommissionPercent());
        Double fee_subcription = Double.valueOf(getFeeSubscription());
        Double fee_marketIntelligenge = Double.valueOf(getFeeMarketIntelligence());
        Double cost_Per_Click = Double.valueOf(getCostPerClick());
        try {
            tierManagementBean.createTier(tierName, commissionPercentage, fee_subcription, fee_marketIntelligenge, cost_Per_Click);
            statusMessage = (tierName + " Created Successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        } catch (ExistException eex) {
            statusMessage = (tierName + " already exists");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void deleteTier(StakeholderTier tier) {
        String tierName = tier.getName();
        try {
            tierManagementBean.deleteTier(tierName);
            setStatusMessage(tierName + " deleted successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        } catch (ExistException eex) { 
            setStatusMessage("Tier got stakeholder therefore it cannot be removed");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        }
    }
    
    public void modifyTier() {   
        tierManagementBean.modifyTier(this.name, this.commissionPercent, this.feeSubscription, this.feeMarketIntelligence, this.costPerClick);
        statusMessage = ("Tier: " + this.name + " Modified Successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Modify Tier Result: " + getStatusMessage(), ""));
    }
    
    public List<String> getNamesOfAllTiers() {
        List<String> returnList = new ArrayList<String>();
        
        for(Object o : tierManagementBean.getAllTiers()){
            StakeholderTier tierEntity = (StakeholderTier)o;
            returnList.add(tierEntity.getName());
        }
        
        return returnList;
        
    }
    
    public List<StakeholderTier> getAllTiers() {
        return tierManagementBean.getAllTiers();
    }
    
    public List<StakeholderTier> getSpecificTier(ActionEvent event) {
        String tier = String.valueOf(getName());
        return tierManagementBean.getSpecificTier(tier);
    }
    
    
    public void updateFields(){
        System.out.println("UpdateFields");
        this.name = selectedTier.getName();
        this.commissionPercent = selectedTier.getCommissionPercent();
        this.feeSubscription = selectedTier.getFeeSubscription();
        this.feeMarketIntelligence = selectedTier.getFeeMarketIntelligence();
        this.costPerClick = selectedTier.getCostPerClick();
    }
    
    /**
     * @return the tierManagementBean
     */
    public TierManagementSBRemote getTierManagementBean() {
        return tierManagementBean;
    }

    /**
     * @param tierManagementBean the tierManagementBean to set
     */
    public void setTierManagementBean(TierManagementSBRemote tierManagementBean) {
        this.tierManagementBean = tierManagementBean;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the commissionPercent
     */
    public Integer getCommissionPercent() {
        return commissionPercent;
    }

    /**
     * @param commissionPercent the commissionPercent to set
     */
    public void setCommissionPercent(Integer commissionPercent) {
        this.commissionPercent = commissionPercent;
    }

    /**
     * @return the feeSubscription
     */
    public Double getFeeSubscription() {
        return feeSubscription;
    }

    /**
     * @param feeSubscription the feeSubscription to set
     */
    public void setFeeSubscription(Double feeSubscription) {
        this.feeSubscription = feeSubscription;
    }

    /**
     * @return the feeMarketIntelligence
     */
    public Double getFeeMarketIntelligence() {
        return feeMarketIntelligence;
    }

    /**
     * @param feeMarketIntelligence the feeMarketIntelligence to set
     */
    public void setFeeMarketIntelligence(Double feeMarketIntelligence) {
        this.feeMarketIntelligence = feeMarketIntelligence;
    }

    /**
     * @return the costPerClick
     */
    public Double getCostPerClick() {
        return costPerClick;
    }

    /**
     * @param costPerClick the costPerClick to set
     */
    public void setCostPerClick(Double costPerClick) {
        this.costPerClick = costPerClick;
    }

    /**
     * @return the statusMessage
     */
    public String getStatusMessage() {
        return statusMessage;
    }

    /**
     * @param statusMessage the statusMessage to set
     */
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    /**
     * @return the selectedTier
     */
    public StakeholderTier getSelectedTier() {
        return selectedTier;
    }

    /**
     * @param selectedTier the selectedTier to set
     */
    public void setSelectedTier(StakeholderTier selectedTier) {
        this.selectedTier = selectedTier;
    }

 
}